<template>
	<view class="page">
    <view class="page-navbar" :style="'background-image: url('+indexBg+');'">
      <u-navbar :fixed="false" title="通知公告" placeholder bgColor="transparent"></u-navbar>
    </view>
    <view class="form-info">
      <view class="form-box u-m-b-48">
        <view class="form-item">
          <view class="form-lable required">标题</view>
          <view class="form-content">
            <u--input placeholder="请输入" border="none" inputAlign="right" v-model="form.title" />
          </view>
        </view>
      </view>
      <view class="form-box u-m-b-48" v-for="(item, index) in gwNoticeFileList" :key="index">
        <view class="form-info-title">教育经历({{ index + 1 }})</view>
        <view class="form-item">
          <view class="form-lable required">内容描述</view>
          <view class="form-content">
            <u--textarea autoHeight maxlength="500" placeholder="请输入" border="none" inputAlign="right" v-model="item.note" />
          </view>
        </view>
        <view class="form-item">
          <view class="form-lable">上传图片</view>
          <view class="form-content-right">
            <u-upload
              :fileList="item.url"
              accept='image'
              name="url"
              :sizeType="['compressed']"
              :maxCount="1"
              @delete="deletePic($event, 'gwNoticeFileList', index)"
              @afterRead="afterRead($event, 'gwNoticeFileList', index)"
              :previewFullImage="true"
            ></u-upload>
          </view>
        </view>
      </view>
      <view class="add-btn" @click="gwNoticeFileList.push({})">
        <image src="https://gowork365.com/prod-api/profile/mini/company/addList.png" />
        <text>添加内容描述</text>
      </view>
      <view class="add-btn u-m-t-28" style="color: #FE748B;" v-if="gwNoticeFileList.length > 1" @click="gwNoticeFileList.pop()">
        <image src="https://gowork365.com/prod-api/profile/mini/company/action5.png" />
        <text>删除内容描述</text>
      </view>
      <view class="save-primary-btn-actions">
        <view class="save-primary-l-btn" @click="jumpTo('/other/staff/staffSelect', {type: '1'})">选择发送对象</view>
        <view class="save-primary-r-btn" @click="submit">保存提交</view>
      </view>
    </view>
  </view>
</template>

<script>
import { getGwnotice, addGwnotice, updateGwnotice } from '@/api/index'
import { uploadImage } from '@/common/util'

export default {
  data() {
    return {
      indexBg: 'https://gowork365.com/prod-api/profile/mini/index/indexBg1.png',
      id: '',
      form: {
        title: '',
      },
      defaultForm: {
        title: '',
      },
      gwNoticeFileList: [],
      checkedList: [],
    }
  },
  onLoad(options) {
    if(options.id) {
      this.id = options.id
      this.getForm()
    } else {
      this.gwNoticeFileList = [{ }]
    }
  },
  methods: {
    getForm() {
      uni.showLoading({ mask: true })
      getGwnotice(this.id).then(response => {
        uni.hideLoading()
        this.form = response.data;
        response.data.gwNoticeFileList.forEach(v => {
          v.url = [ 
            {
              status: "success",
              id: v.id,
              img: this.baseUrl + v.url,
              type: 'url',
              url: this.baseUrl + v.url
            }
          ]
        })
        this.gwNoticeFileList = response.data.gwNoticeFileList;
      });
    },
    submit() {
      const that = this
      if(!this.checkedList.length) {
        uni.showToast({
          icon: 'none',
          title: '请选择员工'
        })
        return
      }
      let formFlag = false
      for(let key in this.defaultForm) {
        if(this.form[key] === '') {
          formFlag = true
        }
      }
      if(formFlag) {
        uni.showToast({
          icon: 'none',
          title: '请完善基础信息'
        })
        return
      }
      let flag1 = false
      that.gwNoticeFileList.forEach(v => {
        if(!v.note) {
          flag1 = true
        }
      })
      if(flag1) {
        uni.showToast({
          icon: 'none',
          title: '请完善内容描述'
        })
        return
      }
      const gwNoticeStaffList = this.checkedList.map(v => {
        return { staffId: v }
      })
      const params = {
        ...this.form,
        gwNoticeFileList: this.gwNoticeFileList,
        gwNoticeStaffList,
      }
      params.gwNoticeFileList.forEach( v=> {
        if(v.url) {
          v.url = v.url.map(v => v.url.replace(this.baseUrl, "")).join()
        }
      })
      uni.showLoading({ mask: true })
      if (this.form.id != null) {
        updateGwnotice(params).then(response => {
          uni.hideLoading()
          uni.showToast({
            icon: 'none',
            title: '修改成功'
          })
          setTimeout(() => {
            that.goBack(1)
          }, 1000)
        });
      } else {
        addGwnotice(params).then(response => {
          uni.hideLoading()
          uni.showToast({
            icon: 'none',
            title: '新增成功'
          })
          setTimeout(() => {
            that.goBack(1)
          }, 1000)
        });
      }
    },
    deletePic(event, name, index=-1) {
      if(index > -1) {
        const info = this[name][index]
        info[event.name].splice(event.index, 1)
        this.$set(this[name], index, info)
        return
      }
      this[name][event.name].splice(event.index, 1);
    },
    // 新增图片
    async afterRead(event, name, index=-1) {
      let lists = [].concat(event.file);
      for (let i = 0; i < lists.length; i++) {
        const res = await uploadImage(lists[0]);
        if(res.code != 200) {
          return
        }
        if(index > -1) {
          const info = this[name][index]
          info[event.name] = [
            {
              status: "success",
              id: res.newFileName,
              img: res.fileName,
              type: event.name,
              url: this.baseUrl + res.fileName
            }
          ]
          this.$set(this[name], index, info)
          return
        } else {
          const img = [
            {
              status: "success",
              id: res.newFileName,
              img: res.fileName,
              type: event.name,
              url: this.baseUrl + res.fileName
            }
          ]
          this.$set(this[name], event.name, img)
        }
      }
    },
  }
}
</script>

<style lang="less" scoped>
.page {
  width: 100%;
  min-height: 100vh;
  background: #F4F7F6;
  position: relative;
  padding-bottom: 188rpx;
  display: flex;
  flex-direction: column;
  .page-navbar {
    background-repeat: no-repeat;
    background-size: 750rpx 690rpx;
  }
  .form-info {
    flex: 1;
    .add-btn {
      width: 686rpx;
      height: 104rpx;
      background: #FFFFFF;
      border-radius: 16rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      gap: 8rpx;
      font-family: PingFangSC, PingFang SC;
      font-size: 28rpx;
      color: rgba(0,0,0,0.85);
      margin: 0 auto 56rpx;
      image {
        width: 24rpx;
        height: 24rpx;
      }
    }
  }
  .tabs-box {
    width: 686rpx;
    height: 96rpx;
    background: #FFFFFF;
    border-radius: 48rpx;
    display: flex;
    margin: 32rpx auto 0;
    .tabs-item {
      flex: 1;
      height: 96rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 48rpx;
      border: 2rpx solid #fff;
    }
    .active {
      border: 2rpx solid #138E88;
      background: #E9F8F6;
      color: #138E88;
    }
  }
}
</style>
